﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Core.Common;
using Jzy.Core.Utility;
using ECommerce.Model.Product;

namespace ECommerce.DAL.Product
{
    public class ECProductCommentDAL : BaseDAL
    {
        public void Hide(List<string> ids)
        {
            string strids = ConvertUtil.ConvertToDbStr(ids);
            string sql = "update ECProductComment set IsDisplay=0 where ID in (" + strids + ")";
            dbOper.RunSql(sql);
        }

        public void Display(List<string> ids)
        {
            string strids = ConvertUtil.ConvertToDbStr(ids);
            string sql = "update ECProductComment set IsDisplay=1 where ID in (" + strids + ")";
            dbOper.RunSql(sql);
        }

        public List<ECProductCommentInfo> GetDisplayList(int productID)
        {
            string sql = "select * from ECProductComment where IsDisplay=1 and ProductID=@p0";
            return dbOper.Query<ECProductCommentInfo>(sql, productID);
        }

        public List<ECProductCommentInfo> GetNewCommentList()
        {
            string sql = "select top 8 * from ECProductComment order by InputTime desc";
            return dbOper.Query<ECProductCommentInfo>(sql);
        }


        public bool HasComment(int productID, int memberID)
        {
            string sql = "select count(*) from ECProductComment where ProductID=@p0 and MemberID=@p1";
            int count = dbOper.GetScalar<int>(sql, productID, memberID);
            return count > 0;
        }

        public void SaveAvgScore(ECProductCommentInfo info)
        {
            string sql = "update ecproduct set avgscore=(select isnull(avg(score),0)  from ecproductcomment where ProductID =" + info.ProductID + ") where id=" + info.ProductID;
            dbOper.RunSql(sql);

        }

        public ECProductCommentInfo GetInfo(int productID, int memberID)
        {
            string sql = "select * from ECProductComment where ProductID=@p0 and MemberID=@p1";
            List<ECProductCommentInfo> list = dbOper.Query<ECProductCommentInfo>(sql, productID, memberID);
            if (list.Count > 0)
            {
                return list.First();
            }
            else
            {
                return null;
            }
        }
    }

}
